package com.wangyi.activity;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wangyi.common.CommonActivity;
import com.wangyi.common.MyHandlerHelper;
import com.wangyi.sdk.OAuth2AccessToken;
import com.wangyi.sdk.PagableResponseList;
import com.wangyi.sdk.TBlog;
import com.wangyi.sdk.TBlogException;
import com.wangyi.sdk.User;
import com.wangyi.util.AsyncImageLoader;
import com.wangyi.util.MySQLiteDatabaseHelper;
import com.wangyi.view.FriendsAdapter;

public class FriendsActivity extends CommonActivity {
	private MySQLiteDatabaseHelper helper;
	private TextView nickname;
	private TextView sex;
	private TextView introduce;
	private TextView friends_counts;
	private TextView followers_counts;
	private TextView statuses_counts;
	private ImageView user_pic;
	private Button search_btn;
	private Button edit_btn;
	private ListView friends_list;
	private User user;
	private Handler handler;
	private PagableResponseList<User> friendsList;
	private ProgressBar pic_img_pro;
	private ProgressBar friends_pro;
	private long user_id;
	private final static int LOAD_USER_PIC = 1;
	private final static int LOAD_FRIENDS = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friends);
		user_id = getIntent().getLongExtra(CURRENT_USERID, 0);
		helper = MySQLiteDatabaseHelper.getInstance(this, MySQLiteDatabaseHelper.ISWRITABLE_DATABASE);
		user = helper.selectAccountById(user_id);
		nickname = (TextView) findViewById(R.id.nickname_tv);
		nickname.setText(getStrFromResource(R.string.nickname_tv) + user.getName());
		sex = (TextView) findViewById(R.id.sex_tv);
		sex.setText(getStrFromResource(R.string.sex_tv) + user.getSex());
		introduce = (TextView) findViewById(R.id.introduce_tv);
		introduce.setText(getStrFromResource(R.string.introduce_tv) + user.getDescription());
		friends_counts = (TextView) findViewById(R.id.friends_count_tv);
		friends_counts.setText(getStrFromResource(R.string.friends_count) + user.getFriends_count());
		followers_counts = (TextView) findViewById(R.id.followers_count_tv);
		followers_counts.setText(getStrFromResource(R.string.followers_count) + user.getFavourites_count());
		statuses_counts = (TextView) findViewById(R.id.statuses_count_tv);
		statuses_counts.setText(getStrFromResource(R.string.statuses_count) + user.getStatuses_count());
		pic_img_pro = (ProgressBar) findViewById(R.id.pic_img_pro);
		friends_pro = (ProgressBar) findViewById(R.id.friends_pro);
		user_pic = (ImageView) findViewById(R.id.pic_img);
		search_btn = (Button) findViewById(R.id.search_btn);
		edit_btn = (Button) findViewById(R.id.edit_btn);
		search_btn.setOnClickListener(this);
		edit_btn.setOnClickListener(this);
		friends_list = (ListView) findViewById(R.id.friends_list);
		handler = new MyHandlerHelper(this) {
			@Override
			public void handleMyMessage(Message msg, WeakReference<Activity> activityWeakRef) {
				if (msg.what == LOAD_FRIENDS) {
					friends_pro.setVisibility(View.GONE);
					friends_list.setVisibility(View.VISIBLE);
					FriendsAdapter adapter = new FriendsAdapter(FriendsActivity.this, friendsList, user_id);
					friends_list.setAdapter(adapter);
				} else if (msg.what == LOAD_USER_PIC) {
					user_pic.setImageBitmap((Bitmap) msg.obj);
					pic_img_pro.setVisibility(View.GONE);
				}
			}
		}.getHandler();
		new Thread(new LoadFriendsThread()).start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_btn:

			break;
		case R.id.edit_btn:

			break;
		case R.id.back_btn:

			break;
		case R.id.rush_btn:

			break;
		}
	}

	public Bitmap loadImageFromUrl(String pic_url) {
		URL url;
		InputStream is = null;
		Bitmap bitmap = null;
		try {
			url = new URL(pic_url);
			URLConnection connection = url.openConnection();
			is = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return AsyncImageLoader.toRoundCorner(bitmap, 5);
	}

	class LoadFriendsThread implements Runnable {
		@Override
		public void run() {
			Bitmap bitmap = loadImageFromUrl(user.getProfile_image_url());
			Message message = handler.obtainMessage(LOAD_USER_PIC, bitmap);
			handler.sendMessage(message);
			TBlog blog = new TBlog(FriendsActivity.this);
			OAuth2AccessToken oauthToken = helper.selectOAuthTokenByUserId(user.getId());
			blog.setOAuth2AccessToken(oauthToken.getAccess_token());
			try {
				friendsList = blog.friends(user_id);
				handler.sendEmptyMessage(LOAD_FRIENDS);
			} catch (TBlogException e) {
				e.printStackTrace();
			}

		}
	}
}
